package com.example.demo.controller;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.util.AjaxResult;
import com.example.demo.util.DateUtil;
import com.example.demo.util.Md5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;



@RestController
@RequestMapping("/user")
@Api(tags="登陆接口")
public class LoginController {

    @Autowired
    private CaptchaService captchaService;

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    public AjaxResult login(@RequestParam("username")String username,
                            @RequestParam("password")String password,
                            @RequestParam("captchaVerification") String captchaVerification) {
        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaVerification(captchaVerification);
        ResponseModel response = captchaService.verification(captchaVO);
        if(response.isSuccess() == false){
            //验证码校验失败，返回信息告诉前端
            //repCode  0000  无异常，代表成功
            //repCode  9999  服务器内部异常
            //repCode  0011  参数不能为空
            //repCode  6110  验证码已失效，请重新获取
            //repCode  6111  验证失败
            //repCode  6112  获取验证码失败,请联系管理员
        }

        User user = new User();
        user.setName(username);
        User user1 = userMapper.selectByPrimaryKey(user);
        if (user1 == null){
            return AjaxResult.error("403","用户未注册");
        }
        user.setPassword(Md5Utils.hash(password));
        User user2 = userMapper.selectByPrimaryKey(user);
        if (user2 == null){
            return AjaxResult.error("402","用户名或密码不正确");
        }
        return AjaxResult.success(user2);
    }

    @PostMapping("/register")
    public AjaxResult register(@RequestParam("username")String username,
                               @RequestParam("password")String password,
                               @RequestParam("email")String email,
                               @RequestParam("phone")String phone,
                               @RequestParam("role")Integer role,
                               @RequestParam("birth")String birth,
                               @RequestParam("nickname")String nickname) {

        User user = new User();
        user.setName(username);
        if (userMapper.selectByPrimaryKey(user) != null){
            return AjaxResult.error("302","该账号已存在");
        }
        user.setPassword(Md5Utils.hash(password));
        user.setEmail(email);
        user.setPhone(phone);
        user.setBrith(birth);
        user.setRole(role);
        user.setNick(nickname);
        user.setCreateTime(DateUtil.getNow());
        int flag = userMapper.insert(user);
        return AjaxResult.success(flag);
    }


    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/selectUser")
    //@ApiOperation(value="查询用户信息", notes="查询用户信息")
    public AjaxResult selectUser(User user) {
        User out = userMapper.selectByPrimaryKey(user);
        return AjaxResult.success(out);
    }


    /**
     * 修改用户信息
     * @return
     */
    @PostMapping("/updateUser")
    //@ApiOperation(value="修改用户信息", notes="修改用户信息")
    public AjaxResult updateUser(User user) {
        //userMapper.selectByPrimaryKey(user);
        return AjaxResult.success(userMapper.updateByPrimaryKey(user));
    }

}
